package me.firstapp.api.shop;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.shop.PointRechargeCard;
import me.firstapp.service.shop.PointRechargeCardService;

@Controller
@RequestMapping(value = "/pointRechargeCard")
@Api(value = "/pointRechargeCard", description = "积分充值卡相关接口")
public class PointRechargeCardApi extends BaseApi {

	@Autowired
	private PointRechargeCardService pointRechargeCardService;

	@RequestMapping(value = "/addPointRechargeCard.do", method = RequestMethod.POST)
	@ApiOperation(value = "积分充值卡新增", notes = "积分充值卡新增")
	public void addPointRechargeCard(Double pointValue, Double price, Integer counts, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			pointRechargeCardService.addPointRechargeCard(pointValue, price, counts);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findPointRechargeCardPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询积分充值卡", notes = "分页查询积分充值卡")
	public void findPointRechargeCardPage(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, HttpServletRequest request, HttpServletResponse response) {
		PageObject<PointRechargeCard> result = new PageObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<PointRechargeCard> pointRechargeCardPage = pointRechargeCardService.findPointRechargeCardPage(pageNo,
					pageSize, searchParams, sortParam, sortType);
			JsonPage<PointRechargeCard> jsonPage = new JsonPage<PointRechargeCard>();
			jsonPage.setList(pointRechargeCardPage.getEntities());
			jsonPage.setPageNo(pointRechargeCardPage.getPageNo());
			jsonPage.setPageSize(pointRechargeCardPage.getPageSize());
			jsonPage.setTotalCount(pointRechargeCardPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/startUseCard.do", method = RequestMethod.POST)
	@ApiOperation(value = "积分充值卡启用", notes = "积分充值卡启用")
	public void startUseCard(String ids, Long startDate, Long endDate, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			pointRechargeCardService.executeStartUse(ids, startDate, endDate);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/startSellCard.do", method = RequestMethod.POST)
	@ApiOperation(value = "积分充值卡设为在售", notes = "积分充值卡设为在售")
	public void startSellCard(Long id, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			pointRechargeCardService.executeStartSell(id);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/stopUse.do", method = RequestMethod.POST)
	@ApiOperation(value = "积分充值卡设为停用", notes = "积分充值卡设为停用")
	public void executeStopUse(Long id, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			pointRechargeCardService.executeStopUse(id);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/selled.do", method = RequestMethod.POST)
	@ApiOperation(value = "积分充值卡设为已售", notes = "积分充值卡设为已售")
	public void selled(Long id, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			// Long apiClientId = (Long) request.getAttribute("apiClientId");
			pointRechargeCardService.executeSelled(id);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
